package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbProdAttaMapper;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdAtta;
import com.bwie.service.TbProdService;
import com.bwie.mapper.TbProdMapper;
import com.bwie.utils.Result;
import com.bwie.utils.TokenUtils;
import com.bwie.vo.PageInfoVo;
import com.bwie.vo.ProdPageVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author lenovo
 * @description 针对表【tb_prod(商品表)】的数据库操作Service实现
 * @createDate 2025-06-20 15:27:23
 */
@Service
public class TbProdServiceImpl extends ServiceImpl<TbProdMapper, TbProd>
        implements TbProdService {

    @Autowired
    TbProdMapper tbProdMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Autowired
    TbProdAttaMapper tbProdAttaMapper;

    @Override
    public Result getProdList(ProdPageVo prodPageVo) {
        //构造分页参数
        Page<TbProd> page = new Page<>(prodPageVo.getPageNum(), prodPageVo.getPageSize());
//        //分页查询
//        QueryWrapper<TbProd> wrapper = new QueryWrapper<>();
//        //根据时间倒序排序
//        wrapper.lambda().orderByDesc(TbProd::getCreatedTime);

        Page<TbProd> tbProdPage = tbProdMapper.getProdList(page, prodPageVo);

        return Result.ok().setData(tbProdPage);
    }

    @Override
    public Result addProd(TbProd tbProd) {
        //SPU和商品名称重复校验
        TbProd prod = tbProdMapper.selectOne(
                new QueryWrapper<TbProd>().lambda()
                        .eq(TbProd::getProdSpu, tbProd.getProdSpu())
                        .or()
                        .eq(TbProd::getProdName, tbProd.getProdName())
        );
        if (prod != null) {
            return Result.error().setMsg("商品已存在");
        }
        String token = request.getHeader("token");
        String userName = TokenUtils.tokenUtilsGetName(token);
        tbProd.setProdStatus(0);
        tbProd.setCreatedBy(userName);
        tbProd.setCreatedTime(new Date());
        tbProdMapper.insert(tbProd);
        return Result.ok();
    }

    @Override
    public Result uploadFile(MultipartFile file) {
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url = "http://192.168.80.192:8888/" + storePath.getFullPath();
            return Result.ok().setData(url);
        } catch (IOException e) {
//            throw new RuntimeException(e);
            return Result.error().setMsg("上传失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addProdAtta(TbProdAtta tbProdAtta) {
        //判断商品是否存在
        TbProd tbProd = tbProdMapper.selectById(tbProdAtta.getProdId());
        if (tbProd == null) {
            return Result.ok().setMsg("商品不存在");
        }
        //删除原有附件  根据商品的ID进行删除
        int delete = tbProdAttaMapper.delete(
                new QueryWrapper<TbProdAtta>().lambda().eq(TbProdAtta::getProdId, tbProdAtta.getProdId())
        );
        tbProdAtta.setCreatedTime(new Date());
        //添加附件
        tbProdAttaMapper.insert(tbProdAtta);

        return Result.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteBatchProd(List<Integer> ids) {
        //批量删除商品

        tbProdMapper.deleteBatchIds(ids);
        //批量删除商品附件

        tbProdAttaMapper.delete(
                new QueryWrapper<TbProdAtta>().lambda().in(TbProdAtta::getProdId, ids)
        );
        return Result.ok();
    }

    @Override
    public Result deleteProd(Integer prodId) {
        //判断商品是存在
        TbProd tbProd = tbProdMapper.selectById(prodId);
        if (tbProd == null) {
            return Result.error().setMsg("商品不存在");
        }
        //删除商品
        tbProdMapper.deleteById(prodId);
        return Result.ok();
    }
}




